From 9f29e9da0994d4ce96a1c4c8c086aaaf405899ed Mon Sep 17 00:00:00 2001 From: "kfraser@localhost.localdomain" Date: Wed, 25 Oct 2006 10:56:50 +0100 Subject: [PATCH] [BLKTAP] Simplify linked-list insertion/deletion. This should also avoid (bogus) compiler warnings, as reported on IA64. Signed-off-by: Keir Fraser --- tools/blktap/drivers/blktapctrl.c | 92 ++++++++++--------------------- tools/blktap/drivers/tapdisk.c | 52 ++++++----------- tools/blktap/drivers/tapdisk.h | 8 +-- 3 files changed, 49 insertions(+), 103 deletions(-) diff --git a/tools/blktap/drivers/blktapctrl.c b/tools/blktap/drivers/blktapctrl.c index 1c27e9eae7..0b00bc4bfd 100644 --- a/tools/blktap/drivers/blktapctrl.c +++ b/tools/blktap/drivers/blktapctrl.c @@ -204,81 +204,49 @@ static blkif_t *test_path(char *path, char **dev, int *type) static void add_disktype(blkif_t *blkif, int type) { - driver_list_entry_t *entry, *ptr, *last; + driver_list_entry_t *entry, **pprev; - if (type > MAX_DISK_TYPES) return; + if (type > MAX_DISK_TYPES) + return; entry = malloc(sizeof(driver_list_entry_t)); entry->blkif = blkif; - entry->next = NULL; - ptr = active_disks[type]; + entry->next = NULL; - if (ptr == NULL) { - active_disks[type] = entry; - entry->prev = NULL; - return; - } - - while (ptr != NULL) { - last = ptr; - ptr = ptr->next; - } + pprev = &active_disks[type]; + while (*pprev != NULL) + pprev = &(*pprev)->next; - /*We've found the end of the list*/ - last->next = entry; - entry->prev = last; - - return; + *pprev = entry; + entry->pprev = pprev; } static int del_disktype(blkif_t *blkif) { - driver_list_entry_t *ptr, *cur, *last; + driver_list_entry_t *entry, **pprev; int type = blkif->drivertype, count = 0, close = 0; - if (type > MAX_DISK_TYPES) return 1; - - ptr = active_disks[type]; - last = NULL; - while (ptr != NULL) { - count++; - if (blkif == ptr->blkif) { - cur = ptr; - if (ptr->next != NULL) { - /*There's more later in the chain*/ - if (!last) { - /*We're first in the list*/ - active_disks[type] = ptr->next; - ptr = ptr->next; - ptr->prev = NULL; - } - else { - /*We're sandwiched*/ - last->next = ptr->next; - ptr = ptr->next; - ptr->prev = last; - } - - } else if (last) { - /*There's more earlier in the chain*/ - last->next = NULL; - } else { - /*We're the only entry*/ - active_disks[type] = NULL; - if(dtypes[type]->single_handler == 1) - close = 1; - } - DPRINTF("DEL_DISKTYPE: Freeing entry\n"); - free(cur); - if (dtypes[type]->single_handler == 0) close = 1; + if (type > MAX_DISK_TYPES) + return 1; - return close; - } - last = ptr; - ptr = ptr->next; + pprev = &active_disks[type]; + while ((*pprev != NULL) && ((*pprev)->blkif != blkif)) + pprev = &(*pprev)->next; + + if ((entry = *pprev) == NULL) { + DPRINTF("DEL_DISKTYPE: No match\n"); + return 1; } - DPRINTF("DEL_DISKTYPE: No match\n"); - return 1; + + *pprev = entry->next; + if (entry->next) + entry->next->pprev = pprev; + + DPRINTF("DEL_DISKTYPE: Freeing entry\n"); + free(entry); + + /* Caller should close() if no single controller, or list is empty. */ + return (!dtypes[type]->single_handler || (active_disks[type] == NULL)); } static int write_msg(int fd, int msgtype, void *ptr, void *ptr2) @@ -592,8 +560,8 @@ int unmap_blktapctrl(blkif_t *blkif) if (del_disktype(blkif)) { close(blkif->fds[WRITE]); close(blkif->fds[READ]); - } + return 0; } diff --git a/tools/blktap/drivers/tapdisk.c b/tools/blktap/drivers/tapdisk.c index 7c88027eb3..859687d8b3 100644 --- a/tools/blktap/drivers/tapdisk.c +++ b/tools/blktap/drivers/tapdisk.c @@ -79,31 +79,17 @@ static void unmap_disk(struct td_state *s) { tapdev_info_t *info = s->ring_info; struct tap_disk *drv = s->drv; - fd_list_entry_t *ptr, *prev; + fd_list_entry_t *entry; drv->td_close(s); if (info != NULL && info->mem > 0) munmap(info->mem, getpagesize() * BLKTAP_MMAP_REGION_SIZE); - ptr = s->fd_entry; - prev = ptr->prev; - - if (prev) { - /*There are entries earlier in the list*/ - prev->next = ptr->next; - if (ptr->next) { - ptr = ptr->next; - ptr->prev = prev; - } - } else { - /*We are the first entry in list*/ - if (ptr->next) { - ptr = ptr->next; - fd_start = ptr; - ptr->prev = NULL; - } else fd_start = NULL; - } + entry = s->fd_entry; + *entry->pprev = entry->next; + if (entry->next) + entry->next->pprev = entry->pprev; close(info->fd); @@ -144,35 +130,29 @@ static inline int LOCAL_FD_SET(fd_set *readfds) return 0; } -static inline fd_list_entry_t *add_fd_entry(int tap_fd, int io_fd[MAX_IOFD], struct td_state *s) +static inline fd_list_entry_t *add_fd_entry( + int tap_fd, int io_fd[MAX_IOFD], struct td_state *s) { - fd_list_entry_t *ptr, *last, *entry; + fd_list_entry_t **pprev, *entry; int i; + DPRINTF("Adding fd_list_entry\n"); /*Add to linked list*/ s->fd_entry = entry = malloc(sizeof(fd_list_entry_t)); entry->tap_fd = tap_fd; - for (i = 0; i < MAX_IOFD; i++) entry->io_fd[i] = io_fd[i]; + for (i = 0; i < MAX_IOFD; i++) + entry->io_fd[i] = io_fd[i]; entry->s = s; entry->next = NULL; - ptr = fd_start; - if (ptr == NULL) { - /*We are the first entry*/ - fd_start = entry; - entry->prev = NULL; - goto finish; - } + pprev = &fd_start; + while (*pprev != NULL) + pprev = &(*pprev)->next; - while (ptr != NULL) { - last = ptr; - ptr = ptr->next; - } - last->next = entry; - entry->prev = last; + *pprev = entry; + entry->pprev = pprev; - finish: return entry; } diff --git a/tools/blktap/drivers/tapdisk.h b/tools/blktap/drivers/tapdisk.h index 1f03156456..238350016b 100644 --- a/tools/blktap/drivers/tapdisk.h +++ b/tools/blktap/drivers/tapdisk.h @@ -191,9 +191,8 @@ static disk_info_t *dtypes[] = { }; typedef struct driver_list_entry { - void *blkif; - void *prev; - void *next; + struct blkif *blkif; + struct driver_list_entry **pprev, *next; } driver_list_entry_t; typedef struct fd_list_entry { @@ -201,8 +200,7 @@ typedef struct fd_list_entry { int tap_fd; int io_fd[MAX_IOFD]; struct td_state *s; - void *prev; - void *next; + struct fd_list_entry **pprev, *next; } fd_list_entry_t; int qcow_create(const char *filename, uint64_t total_size, -- 2.30.2